//
//  SideViewController.m
//  恒记
//
//  Created by can on 16/5/5.
//  Copyright © 2016年 can. All rights reserved.
//

#import "SideViewController.h"
@interface SideViewController ()
{
    BOOL _isOpenAndClose;
    CGPoint openPointCenter;
}
@property (nonatomic,strong) UIViewController *homeVc;
- (void)initializeDataSource;/**< 初始化数据源 */
- (void)initializeUserInterface;/**< 初始化用户界面 */

@end

@implementation SideViewController
-(instancetype)initWithContentViewController:(UIViewController *)contentVc HomeViewController:(UIViewController *)homVC{
    if (self = [super init]) {
        self.homeVc = homVC;
        self.contentVc = contentVc;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
    self.view.backgroundColor = [UIColor whiteColor];
    openPointCenter = CGPointMake(self.view.center.x + P_width * 0.6,
                                  self.view.center.y);
    
    
}

#pragma mark *** Initialize ***
-(void)initializeDataSource{
    

}

-(void)initializeUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //加载主界面
    [self addChildViewController:self.contentVc];
    [self addChildViewController:self.homeVc];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(handlePan:)];
    [self.view addGestureRecognizer:panGestureRecognizer];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(handleTap:)];
    
    [self.homeVc.view addGestureRecognizer:tapGestureRecognizer];
    
    // 1、滑动手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToSwipeGestureRecognizer:)];
    // 设置滑动方向
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeGesture];
    
    UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeGestureRecognizer];
}

#pragma mark *** Override ***
-(void)addChildViewController:(UIViewController *)childController{
    [super addChildViewController:childController];
    [self.view addSubview:childController.view];
    [childController didMoveToParentViewController:self];
}


#pragma mark *** Events ***
-(void)openAndCloseSideVcToEvent{
    if (!_isOpenAndClose) {
        CGFloat centerX = CGRectGetMidX(self.view.bounds);
        CGFloat centerY = CGRectGetMidY(self.view.bounds);
        [UIView animateWithDuration:0.3 animations:^{
            self.homeVc.view.center = CGPointMake(centerX - SCREEN_WIDTH*3/5, centerY);
            self.contentVc.view.center  = CGPointMake(centerX, centerY);
            self.contentVc.view.alpha = 1.0;
        }];
    }else{
        CGFloat centerX = CGRectGetMidX(self.view.bounds);
        CGFloat centerY = CGRectGetMidY(self.view.bounds);
        [UIView animateWithDuration:0.3 animations:^{
            self.homeVc.view.center = CGPointMake(centerX, centerY);
            self.contentVc.view.center = CGPointMake(self.contentVc.view.center.x + SCREEN_WIDTH*3/5, self.contentVc.view.center.y);
            self.contentVc.view.alpha = 0.0;
        }];
    }
    
    _isOpenAndClose = !_isOpenAndClose;
    
}

#pragma mark *** Getters ***
//手势
//拖拽
-(void) handlePan:(UIPanGestureRecognizer*) recognizer
{
    CGPoint translation = [recognizer translationInView:self.view];
    CGFloat centerX = CGRectGetMidX(self.view.bounds);
    CGFloat centerY = CGRectGetMidY(self.view.bounds);
    float x = self.contentVc.view.center.x + translation.x;
    //    NSLog(@"translation x:%f", translation.x);
    if (x < self.view.center.x) {
        x = self.view.center.x;
    }else if (x > openPointCenter.x){
        x = openPointCenter.x;
    }
    self.contentVc.view.center = CGPointMake(x, openPointCenter.y);
    self.homeVc.view.center = CGPointMake(x - SCREEN_WIDTH * 0.6, openPointCenter.y);
    self.contentVc.view.alpha = (openPointCenter.x - self.contentVc.view.center.x) / 0.6 * SCREEN_WIDTH;
    
    if(recognizer.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.3
                              delay:0.01
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^(void)
         {
             if (x > openPointCenter.x - P_width * 0.6 / 2) {
                 self.contentVc.view.center = openPointCenter;
                 self.homeVc.view.center = CGPointMake(centerX, centerY);
                 self.contentVc.view.alpha = 0.0;
             }else{
                 self.homeVc.view.center = CGPointMake(centerX - SCREEN_WIDTH*3/5, centerY);
                 self.contentVc.view.center  = CGPointMake(centerX, centerY);
                 self.contentVc.view.alpha = 1.0;             }
             
         }completion:^(BOOL isFinish){
             
         }];
    }
    
    [recognizer setTranslation:CGPointZero inView:self.view];
}
//点击关闭
-(void) handleTap:(UITapGestureRecognizer*) recognizer
{
    CGFloat centerX = CGRectGetMidX(self.view.bounds);
    CGFloat centerY = CGRectGetMidY(self.view.bounds);
    [UIView animateWithDuration:0.3
                          delay:0.01
                        options:UIViewAnimationOptionTransitionCurlUp animations:^(void){
                            self.homeVc.view.center = CGPointMake(centerX, centerY);
                            self.contentVc.view.center = CGPointMake(self.contentVc.view.center.x + SCREEN_WIDTH*3/5, self.contentVc.view.center.y);
                            self.contentVc.view.alpha = 0.0;
                            
                        }completion:nil];
    
}
// 滑动手势 (打开)
- (void)respondsToSwipeGestureRecognizer:(UISwipeGestureRecognizer *)gesture {
    CGFloat centerX = CGRectGetMidX(self.view.bounds);
    CGFloat centerY = CGRectGetMidY(self.view.bounds);
    if (gesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"滑动手势开始");
    }else if (gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"滑动手势持续中");
    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"滑动手势结束");
        [UIView animateWithDuration:0.3 animations:^{
            self.homeVc.view.center = CGPointMake(centerX, centerY);
            self.contentVc.view.center = CGPointMake(self.contentVc.view.center.x + SCREEN_WIDTH*3/5, self.contentVc.view.center.y);
            self.contentVc.view.alpha = 0.0;
        }];
    }
}
//关闭
- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture  {
    CGFloat centerX = CGRectGetMidX(self.view.bounds);
    CGFloat centerY = CGRectGetMidY(self.view.bounds);
    if (gesture.state == UIGestureRecognizerStateBegan) {
        NSLog(@"滑动手势开始");
    }else if (gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"滑动手势持续中");
    }else if (gesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"滑动手势结束");
        [UIView animateWithDuration:0.3 animations:^{
            self.homeVc.view.center = CGPointMake(centerX - SCREEN_WIDTH*3/5, centerY);
            self.contentVc.view.center  = CGPointMake(centerX, centerY);
            self.contentVc.view.alpha = 1.0;
        }];
    }
}

@end
